{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 Numpy介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NumPy(Numerical Python) 是 Python 语言的一个扩展程序库，支持大量的维度数组与矩阵运算，此外也针对数组运算提供大量的数学函数库。    \n",
    "NumPy 是一个运行速度非常快的数学库，主要用于数组计算，具体：\n",
    "1. 一个强大的N维数组对象 ndarray\n",
    "2. 广播功能函数\n",
    "3. 整合 C/C++/Fortran 代码的工具\n",
    "4. 线性代数、傅里叶变换、随机数生成等功能\n",
    "\n",
    "\n",
    "SciPy 是一个开源的 Python 算法库和数学工具包。SciPy 包含的模块有最优化、线性代数、积分、插值、特殊函数、快速傅里叶变换、信号处理和图像处理、常微分方程求解和其他科学与工程中常用的计算。    \n",
    "\n",
    "Matplotlib 是 Python 编程语言及其数值数学扩展包 NumPy 的可视化操作界面。它为利用通用的图形用户界面工具包，如 Tkinter, wxPython, Qt 或 GTK+ 向应用程序嵌入式绘图提供了应用程序接口（API）。\n",
    "\n",
    "参考：    \n",
    "https://www.runoob.com/numpy/numpy-tutorial.html     \n",
    "http://c.biancheng.net/numpy/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 Ndarray 对象 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NumPy 定义了一个 n 维数组对象，是一个一系列相同类型元素组成的数组集合。数组中的每个元素都占有大小相同的内存块，并且可以使用索引或切片的方式获取数组中的每个元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------\n",
      "[('name', 'S20'), ('age', 'i1')]\n",
      "[(b'Zhangsan', 18) (b'Wuji', 28)]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# numpy.array(object, dtype = None, copy = True, order = None,ndmin = 0)\n",
    "exam = np.array([[1,2,3],[4,5,6]])\n",
    "print(exam)\n",
    "\n",
    "print(\"--------------\")\n",
    "\n",
    "# 定义结构化数据\n",
    "Stu = np.dtype([('name','S20'), ('age', 'i1')])\n",
    "print(Stu)\n",
    "stu_exam = np.array([('Zhangsan', 18),('Wuji', 28)], dtype = Stu) \n",
    "print(stu_exam)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数组的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3)\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "print(exam.shape)\n",
    "print(exam.ndim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 索引与切片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ndarray对象的内容可以通过索引或切片来访问和修改，与 Python 中 list 的切片操作一样。    \n",
    "ndarray 数组可以基于 0 - n 的下标进行索引，切片对象可以通过内置的 slice 函数，并设置 start, stop 及 step 参数进行，从原数组中切割出一个新数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [3 4 5]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "# 举例\n",
    "a = np.array([[1,2,3],[3,4,5],[4,5,6]])\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:2][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0][2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 4 5]\n"
     ]
    }
   ],
   "source": [
    "# 整数数组索引\n",
    "# 获取数组中(0,0)，(1,1)和(2,0)位置处的元素\n",
    "x = np.array([[1,  2],  [3,  4],  [5,  6]]) \n",
    "y = x[[0,1,2],  [0,1,0]]  \n",
    "print (y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数学函数\n",
    "1. NumPy 提供了标准的三角函数：sin()、cos()、tan()。\n",
    "2. 舍入函数：numpy.around() 函数返回指定数字的四舍五入值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1.   6. 123.   1.  26.]\n"
     ]
    }
   ],
   "source": [
    "# numpy.around(a,decimals)  decimals: 舍入的小数位数\n",
    "a = np.array([1.0,  5.55, 123, 0.567, 25.532])  \n",
    "print(np.around(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy.floor() 返回小于或者等于指定表达式的最大整数，即向下取整。     \n",
    "numpy.ceil() 返回大于或者等于指定表达式的最小整数，即向上取整。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "算数函数   \n",
    "1. NumPy 算术函数包含简单的加减乘除: add()，subtract()，multiply() 和 divide()。    \n",
    "注意：数组必须具有相同的形状或符合数组广播规则。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  11  102 1003]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([10,100,1000])  \n",
    "b = np.array([1,2,3])  \n",
    "print (np.add(a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "统计函数    \n",
    "1. numpy.amin() 用于计算数组中的元素沿指定轴的最小值。\n",
    "2. numpy.amax() 用于计算数组中的元素沿指定轴的最大值。\n",
    "3. numpy.ptp()函数计算数组中元素最大值与最小值的差（最大值 - 最小值）。\n",
    "4. numpy.percentile() 百分位数是统计中使用的度量，表示小于这个值的观察值的百分比。\n",
    "5. numpy.median() 函数用于计算数组 a 中元素的中位数（中值）\n",
    "6. numpy.mean() 函数返回数组中元素的算术平均值。如果提供了轴，则沿其计算。算术平均值是沿轴的元素的总和除以元素的数量。\n",
    "7. numpy.average() 函数根据在另一个数组中给出的各自的权重计算数组中元素的加权平均值。\n",
    "8. 标准差、方差：np.std\\np.var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n",
      "[6.5 4.5 2.5]\n",
      "[7. 2.]\n"
     ]
    }
   ],
   "source": [
    "# numpy.percentile() \n",
    "a = np.array([[10, 7, 4], [3, 2, 1]])\n",
    "# 50% 的分位数\n",
    "print (np.percentile(a, 50))\n",
    "# axis 为 0，在纵列上求\n",
    "print (np.percentile(a, 50, axis=0)) \n",
    "# axis 为 1，在横行上求\n",
    "print (np.percentile(a, 50, axis=1)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "2.0\n"
     ]
    }
   ],
   "source": [
    "# numpy.average()\n",
    "a = np.array([1,2,3,4]) \n",
    "# 若不指定权重相当于对数组求均值\n",
    "print (np.average(a))\n",
    "\n",
    "we = np.array([4,3,2,1]) \n",
    "print(np.average(a,weights = we))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.4 NumPy 中的广播机制（Broadcast）\n",
    "旨在解决不同形状数组之间的算术运算问题。   \n",
    "这种机制的核心是对形状较小的数组，在横向或纵向上进行一定次数的重复，使其与形状较大的数组拥有相同的维度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [11 12 13]\n",
      " [21 22 23]\n",
      " [31 32 33]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[ 0, 0, 0],\n",
    "           [10,10,10],\n",
    "           [20,20,20],\n",
    "           [30,30,30]])\n",
    "\n",
    "b = np.array([1,2,3])\n",
    "print(a + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
